/*
 * @(#)2020年5月27日 V1.0
 * 
 * Copyright 2020 WELL-SOFT, Inc. All rights reserved.
 */
package com.wellsoft.pt.app.dingtalk.support;

import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.dingtalk.api.request.OapiWorkrecordAddRequest.FormItemVo;
import com.google.common.collect.Lists;
import com.wellsoft.context.util.date.DateUtils;
import com.wellsoft.pt.app.dingtalk.constants.DingTalkInfo;
import com.wellsoft.pt.app.dingtalk.entity.MultiOrgDingUserInfo;
import com.wellsoft.pt.app.dingtalk.service.MultiOrgDingUserInfoService;
import com.wellsoft.pt.app.dingtalk.service.WorkRecordService;
import com.wellsoft.pt.app.dingtalk.utils.DingtalkApiUtils;
import com.wellsoft.pt.app.dingtalk.utils.DingtalkUtils;
import com.wellsoft.pt.bpm.engine.entity.FlowInstance;
import com.wellsoft.pt.bpm.engine.entity.TaskInstance;
import com.wellsoft.pt.bpm.engine.service.FlowService;
import com.wellsoft.pt.bpm.engine.service.TaskService;
import com.wellsoft.pt.jpa.event.WellptEventListener;
import com.wellsoft.pt.multi.org.entity.MultiOrgUserAccount;
import com.wellsoft.pt.multi.org.facade.service.MultiOrgUserService;
import com.wellsoft.pt.security.util.SpringSecurityUtils;
import com.wellsoft.pt.workflow.event.WorkTodoEvent;

/**
 * Description: 如何描述该类
 *  
 * @author zhongzh
 * @date 2020年5月27日
 * @version 1.0
 * 
 * <pre>
 * 修改记录:
 * 修改后版本	修改人		修改日期			修改内容
 * 2020年5月27日.1	zhongzh		2020年5月27日		Create
 * </pre>
 *
 */
@Component
public class WorkRecordSendListener extends WellptEventListener<WorkTodoEvent> {

	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private TaskService taskService;

	@Autowired
	private FlowService flowService;

	@Autowired
	private WorkRecordService workRecordService;

	@Autowired
	private MultiOrgUserService multiOrgUserService;

	@Autowired
	private MultiOrgDingUserInfoService multiOrgDingUserInfoService;

	/** 
	 * 
	 */
	@Override
	public void onApplicationEvent(WorkTodoEvent event) {
		String flowInstUuid = null, taskInstUuid = null;
		FlowInstance flowInstance = event.getFlowInstance();
		TaskInstance taskInstance = event.getTaskInstance();
		if ((flowInstUuid = flowInstance.getUuid()) == null || (taskInstUuid = taskInstance.getUuid()) == null) {
			return;// 流程数据未持久化（比如办理人异常）
		}
		Set<String> userIds = event.getUserIds();
		String authUri, title = flowInstance.getTitle();
		// 构建跳转url
		authUri = DingTalkInfo.CORP_DOMAIN_URI + "/mobile/pt/dingtalk/auth?uri=%s";
		String redirectUri = DingtalkUtils.urlEncode(DingtalkUtils.uriFormat(
				"/workflow/mobile/work/view/todo2?flowInstUuid=%s&taskInstUuid=%s&title=%s", flowInstUuid, taskInstUuid, title));
		// 构建formItemList
		List<FormItemVo> formItemList = Lists.newArrayList();
		FormItemVo itemTaskName = new FormItemVo();
		itemTaskName.setTitle("办理环节");
		itemTaskName.setContent(taskInstance.getName());
		formItemList.add(itemTaskName);
		FormItemVo itemOperator = new FormItemVo();
		itemOperator.setTitle("办理人");
		String currentUserName = SpringSecurityUtils.getCurrentUserName();
		itemOperator.setContent(currentUserName);
		formItemList.add(itemOperator);
		FormItemVo itemCreateTime = new FormItemVo();
		itemCreateTime.setTitle("开始时间");
		itemCreateTime.setContent(DateUtils.formatDateTimeMin(taskInstance.getStartTime()));
		formItemList.add(itemCreateTime);
		for (String userId : userIds) {
			MultiOrgDingUserInfo multiOrgDingUserInfo = multiOrgDingUserInfoService.getByUserId(userId);
			if (multiOrgDingUserInfo == null) {
				logger.warn("flowInstUuid[" + flowInstUuid + "]taskInstUuid[" + taskInstUuid + "]userId[" + userId
						+ "]未找到钉钉用户");
				continue;
			}
			String dingUserId = multiOrgDingUserInfo.getDing_userId();
			MultiOrgUserAccount account = multiOrgUserService.getAccountByUserId(userId);
			String userName = account == null ? null : account.getUserName();
			DingtalkApiUtils.addWorkRecord(taskInstUuid, dingUserId, userName, DingtalkApiUtils.getAccessToken(),
					title, DingtalkUtils.uriFormat(authUri, redirectUri), formItemList);
		}
	}

}
